package com.zhong.gang.app.ui.audit;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.tools.ToastUtils;
import com.zhong.gang.app.R;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.api.HttpUtils;
import com.zhong.gang.app.databinding.ActivityExamineBinding;
import com.zhong.gang.app.ui.examine.ExamineAdapter;
import com.zhong.gang.app.ui.examine.ExamineBean;
import com.zhong.gang.app.ui.examine.presenter.ExaminePresenter;
import com.zhong.gang.applib.base.act.ActBaseRefresh;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.callback.Callback;
import com.zhong.gang.applib.modle.ListBean;
import com.zhong.gang.applib.pop.TypeBean;
import com.zhong.gang.applib.pop.TypePop;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;


/**
 * @author Yan
 * @date 2020/6/9  15:02
 * @describe 填写此类描述
 * <p>
 * ====================================================
 */
public class AuditListAct extends ActBaseRefresh<ActivityExamineBinding, AuditAdapter>
        implements AuditAdapter.OnItemClickListener, TypePop.onTypeItemClickedListener,
        PopupWindow.OnDismissListener, BaseQuickAdapter.OnItemChildClickListener {
    protected TypePop typePop, statusPop;
    private int type = -1, status = -1;
    private ExaminePresenter presenter;
    private List<AuditBean> mData = new ArrayList<AuditBean>();


    @Override
    public void onDismiss() {
        bind.typeImg.setImageResource(com.zhong.gang.applib.R.mipmap.icon_down);
        bind.statusImg.setImageResource(com.zhong.gang.applib.R.mipmap.icon_down);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        AuditBean data = (AuditBean) adapter.getItem(position);
        if (null != data) {
            Intent intent = new Intent(this, AuditDetailAct.class);
            intent.putExtra("AuditId", data.getId());
            startActivity(intent);
        }
    }

    @Override
    public int LayoutRes() {
        return R.layout.activity_examine;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "财务审核";
    }

    @Override
    public void initView() {
        presenter = new ExaminePresenter();
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initrefrsh(bind.pullto, new AuditAdapter(), bind.recyclerView);
        adapter.setOnItemClickListener(this);
        adapter.setOnItemChildClickListener(this);
        bind.setAct(this);
        initPop();
        getData();

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        AuditBean data = (AuditBean) adapter.getItem(position);
        if (null != data) {
            int status = data.getAuditStatus();
            if (0 == status || 1 == status) {
                data.setCheck(!data.isCheck());
                adapter.notifyItemChanged(position);
            } else {
                ToastUtils.s(this, "该订单不能审核");
            }

        }
    }

    /**
     * 初始话pop和dialog
     */
    private void initPop() {
        List<TypeBean> typeList = new ArrayList<>();
        getTypeList(typeList, "全部", -1);
        getTypeList(typeList, "外调货业", 1);
        getTypeList(typeList, "驻场接单", 2);
        getTypeList(typeList, "料场收货", 4);
        getTypeList(typeList, "派车单", 8);
        typePop = new TypePop(this, bind.typeLayout, typeList, this);


        List<TypeBean> statusList = new ArrayList<>();
        getTypeList(statusList, "全部", -1);
        getTypeList(statusList, "未审核", 0);
        getTypeList(statusList, "审核中", 1);
        getTypeList(statusList, "审核通过", 2);
        getTypeList(statusList, "审核未通过", 3);
        statusPop = new TypePop(this, bind.statusLayout, statusList, this);

        typePop.setOnDismissListener(this);
        statusPop.setOnDismissListener(this);
    }

    /**
     * 获取typeList
     *
     * @param data
     * @param title
     * @param type
     */
    protected void getTypeList(List<TypeBean> data, String title, int type) {
        if (data == null) {
            return;
        }
        data.add(new TypeBean(title, type));
    }

    @Override
    public void onTypeItem(View view, TypeBean typeBean) {
        if (typeBean == null) {
            return;
        }
        int i = view.getId();
        if (i == R.id.type_layout) {
            type = typeBean.getType();
            bind.typeTv.setText(typeBean.getTitle());
        } else if (i == R.id.status_layout) {
            status = typeBean.getType();
            bind.statusTv.setText(typeBean.getTitle());
        }
        onRefresh();
    }

    @Override
    public void getData() {
        super.getData();
        presenter.getAuditOrderPayApply(this, page, "", -1, status,
                -1, new ActCallBack<BaseDataBean<ListBean<AuditBean>>>() {
                    @Override
                    public void onResponse(BaseDataBean<ListBean<AuditBean>> data)
                            throws Exception {
                        super.onResponse(data);
                        mData = data.getResult().getRecords();
                        setData(mData);
                    }
                });
    }

    /**
     * 点击事件
     *
     * @param view
     */
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.type_layout:
                bind.typeImg.setImageResource(com.zhong.gang.applib.R.mipmap.icon_up);
                typePop.showAsDropDown(bind.typeLayout, type);
                break;
            case R.id.status_layout:
                bind.statusImg.setImageResource(com.zhong.gang.applib.R.mipmap.icon_up);
                statusPop.showAsDropDown(bind.statusLayout, status);
                break;
            case R.id.audit_all:
                if (mData == null || mData.isEmpty()) {
                    ToastUtils.s(this, "数据为空");
                    return;
                }
                StringBuilder builder = new StringBuilder();
                StringBuilder statusBuilder = new StringBuilder();
                boolean check = checkData();
                if (check) {
                    ToastUtils.s(this, "请选择审核状态一致的订单");
                    return;
                }

                for (AuditBean mDatum : mData) {
                    if (mDatum.isCheck()) {
                        builder.append(mDatum.getId()).append(",");
                        statusBuilder.append(1).append(",");
                    }
                }
                int len =builder.length();
                if (len<=0){
                    ToastUtils.s(this, "请选择需要审核的订单");
                    return;
                }
                HttpUtils.post(Api.ORDER_PAY_APPLY_AUDIT, this)
                         .addParams("ids", builder.deleteCharAt(builder.length() - 1).toString())
                         .addParams("audits",
                                 statusBuilder.deleteCharAt(statusBuilder.length() - 1).toString())
                         .build().execute(new ActCallBack<BaseDataBean>() {
                    @Override
                    public void onResponse(BaseDataBean data) throws Exception {
                        super.onResponse(data);
                        onRefresh();
                    }
                });
                break;
            default:
                break;
        }
    }

    private boolean checkData() {
        Set<Integer> set = new HashSet<Integer>();
        for (AuditBean mDatum : mData) {
            if (mDatum.isCheck()) {
                set.add(mDatum.getAuditStatus());
            }
        }

        return set.size() > 1;
    }
}
